import request from '@/utils/request';
import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import type { EditETCTableItem, ETCTableItem, ReportETCRecordTableItem } from './types.d';

export async function queryETCList(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ETCTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    list: ETCTableItem[];
  }> = await request('/api/etc/v1.0.0/common/list', {
    params: { data, sort },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function createETC(params: EditETCTableItem) {
  const res = await request('/api/etc/v1.0.0/common/info', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}

export async function updateETC(id: string, params: EditETCTableItem) {
  if (!id) return { success: false, message: 'id不能为空' };
  const res = await request(`/api/etc/v1.0.0/common/info/${id}`, {
    method: 'PUT',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '更新成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function deleteETC(id: string) {
  if (!id) return { success: false, message: 'id不能为空' };
  const res = await request(`/api/etc/v1.0.0/common/info/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '删除成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '删除失败' };
}

export async function queryETCReportList(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ReportETCRecordTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    list: ReportETCRecordTableItem[];
  }> = await request('/api/etc/v1.0.0/common/report/list', {
    params: { data, sort },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function cancelETC(id: string) {
  const res = await request(`/api/etc/v1.0.0/common/report/revoke/${id}`, {
    method: 'POST',
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '撤销成功',
    };
  return { data: res?.data, success: false, message: res?.message || '撤销失败' };
}
